package com.ruoyi.hospital.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hospital.mapper.SchedulesMapper;
import com.ruoyi.hospital.service.AppointmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.AppointmentsMapper;
import com.ruoyi.hospital.domain.Appointments;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约信息Service业务层处理
 *
 * @author xiaozhao
 * @date 2025-07-04
 */
@Service
public class AppointmentsServiceImpl implements AppointmentsService
{
    @Autowired
    private AppointmentsMapper appointmentsMapper;

    @Autowired
    private SchedulesMapper schedulesMapper;

    /**
     * 查询预约信息
     *
     * @param appointmentId 预约信息主键
     * @return 预约信息
     */
    @Override
    public Appointments selectAppointmentsByAppointmentId(Long appointmentId)
    {
        return appointmentsMapper.selectAppointmentsByAppointmentId(appointmentId);
    }

    /**
     * 查询预约信息列表
     *
     * @param appointments 预约信息
     * @return 预约信息
     */
    @Override
    public List<Appointments> selectAppointmentsList(Appointments appointments)
    {
        return appointmentsMapper.selectAppointmentsList(appointments);
    }

    /**
     * 新增预约信息
     *
     * @param appointments 预约信息
     * @return 结果
     */
    @Override
    public int insertAppointments(Appointments appointments)
    {
        appointments.setCreateTime(DateUtils.getNowDate());
        return appointmentsMapper.insertAppointments(appointments);
    }

    /**
     * 修改预约信息
     *
     * @param appointments 预约信息
     * @return 结果
     */
    @Override
    public int updateAppointments(Appointments appointments)
    {
        appointments.setUpdateTime(DateUtils.getNowDate());
        return appointmentsMapper.updateAppointments(appointments);
    }

    /**
     * 批量删除预约信息
     *
     * @param appointmentIds 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentsByAppointmentIds(Long[] appointmentIds)
    {
        return appointmentsMapper.deleteAppointmentsByAppointmentIds(appointmentIds);
    }

    /**
     * 删除预约信息信息
     *
     * @param appointmentId 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentsByAppointmentId(Long appointmentId)
    {
        return appointmentsMapper.deleteAppointmentsByAppointmentId(appointmentId);
    }

    @Transactional
    @Override
    public int insertAppointmentsWithQuotaUpdate(Appointments appointments) {
        appointments.setCreateTime(DateUtils.getNowDate());
        int result = appointmentsMapper.insertAppointments(appointments);
        if (result > 0) {
            // 更新号源数量
            schedulesMapper.updateQuota(appointments.getDoctorId(), appointments.getAppointmentDate(), appointments.getTimeSlot(), -1);
        }
        return result;
    }

    @Transactional
    @Override
    public int updateAppointmentsWithQuotaUpdate(Appointments appointments) {
        appointments.setUpdateTime(DateUtils.getNowDate());
        // 先获取原有的预约信息
        Appointments oldAppointment = appointmentsMapper.selectAppointmentsByAppointmentId(appointments.getAppointmentId());
        int result = appointmentsMapper.updateAppointments(appointments);
        if (result > 0) {
            // 恢复原有的号源数量
            schedulesMapper.updateQuota(oldAppointment.getDoctorId(), oldAppointment.getAppointmentDate(), oldAppointment.getTimeSlot(), 1);
            // 更新新的号源数量
            schedulesMapper.updateQuota(appointments.getDoctorId(), appointments.getAppointmentDate(), appointments.getTimeSlot(), -1);
        }
        return result;
    }
}
